import request from '@/utils/request'
import custom from '@/utils/custom'
import { getCampusSelector } from '@/api/campus/campus'
import ExportBox from './ExportBox'
export default {
  name: 'train_notsign',
  components: { ExportBox },
  data() {
    return {
      showAll: false,
      query: {
        title: undefined,
        certificateTemplate: undefined,
        registStartDate: [],
        brand: undefined,
        provinceId: undefined,
        company: undefined,
        campusLevel: [],
        campusStatus: []
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 50020,
        sort: 'desc',
        sidx: ''
      },
      exportBoxVisible: false,
      columnList: [{ prop: 'code', label: '校区编码' }, { prop: 'shortName', label: '校区名称' }, { prop: 'typeText', label: '校区类型' }, { prop: 'manager', label: '校区负责人' }, { prop: 'managerMobile', label: '负责人电话' }, { prop: 'startDate', label: '服务开始时间' }, { prop: 'endDate', label: '服务结束时间' }, { prop: 'statusText', label: '校区状态' }, { prop: 'province', label: '省份' }, { prop: 'levelText', label: '校区阶段' }, { prop: 'brand', label: '品牌中文' }, { prop: 'company', label: '所属区域' }],
      campusOptions: [],
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const today = new Date()
              let daterange = custom.getLastWeekDays(today, 1)
              picker.$emit('pick', [new Date(daterange.starttime), today])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const today = new Date()
              let daterange = custom.getLastMonthDays(today, 1)
              picker.$emit('pick', [new Date(daterange.starttime), today])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const today = new Date()
              let daterange = custom.getLastMonthDays(today, 3)
              picker.$emit('pick', [new Date(daterange.starttime), today])
            }
          }
        ]
      },
      statusOptions: [],
      certificateTemplateOptions: [],
      brandOptions: [],
      levelOptions: [],
      companyOptions: [{ enCode: '总部', fullName: '总部' }, { enCode: '河北分公司', fullName: '河北分公司' }, { enCode: '山东分公司', fullName: '山东分公司' }, { enCode: '西安分公司', fullName: '西安分公司' }],

      campusTypeOptions: []
    }
  },
  computed: {},
  created() {
    this.listLoading = false

    getCampusSelector().then(res => {
      this.campusOptions = res.data
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'TrainCertificateType' }).then(res => {
      this.certificateTemplateOptions = res.filter(t => t.enabledMark == 1)
      console.log(this.certificateTemplateOptions)
      this.query.certificateTemplate = this.certificateTemplateOptions[0].children[0].enCode
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
      this.query.brand = this.brandOptions[0].enCode
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'CampusLevel' }).then(res => {
      this.levelOptions = res
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'CampusType' }).then(res => {
      this.campusTypeOptions = res.filter(t => t.enabledMark == 1)
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'CampusStatus' }).then(res => {
      this.statusOptions = res
    })
  },
  methods: {
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == 'ascending' ? 'asc' : 'desc'
      this.listQuery.sidx = !order ? '' : prop
      this.initData()
    },

    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          // 对于数组类型的参数，campusStatus 和 campusLevel 直接传递数组给后台
          if (key === 'campusStatus' || key === 'campusLevel') {
            query[key] = _query[key]
          } else {
            query[key] = _query[key].join()
          }
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/train/withoutorders`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },

    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    download(data) {
      let _query = { ...data, ...this.listQuery, ...this.query }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          // 对于数组类型的参数，campusStatus 和 campusLevel 直接传递数组给后台
          if (key === 'campusStatus' || key === 'campusLevel') {
            query[key] = _query[key]
          } else {
            query[key] = _query[key].join()
          }
        } else {
          query[key] = _query[key]
        }
      }

      request({
        url: `/api/campus/train/Actions/Export`,
        method: 'GET',
        data: query
      }).then(res => {
        this.$message({
          message: '文件下载链接稍后推送到站内信，请在右上角铃铛图标处查收！',
          type: 'success',
          showClose: true,
          duration: 0
        })
        this.$refs.ExportBox.visible = false
        this.exportBoxVisible = false
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 50,
        sort: 'desc',
        sidx: ''
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 50,
        sort: 'desc',
        sidx: ''
      }
      this.defaultDate()
      this.initData()
    },
    defaultDate() {
      let today = new Date()
      var date = new Date(today.setDate(today.getDate() + 1))
      let daterange = custom.getLastMonthDays(today, 1)
      this.query.createTime = [daterange.starttime, custom.dateFormat(date, 'YYYY-MM-DD')] //将值设置给插件绑定的数据
    }
  }
}
